package com.jiaming.employee.salary.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiaming.employee.attendance.bean.vo.DayVO;
import com.jiaming.employee.common.bean.vo.MyPageVO;
import com.jiaming.employee.department.bean.pojo.Department;
import com.jiaming.employee.department.service.IDepartmentService;
import com.jiaming.employee.salary.bean.pojo.Salary;
import com.jiaming.employee.salary.bean.vo.GetSalaryPageVO;
import com.jiaming.employee.salary.mapper.SalaryMapper;
import com.jiaming.employee.salary.service.ISalaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author dragon
 * @create 2021-05-14 21:13
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {

    @Autowired
    IDepartmentService departmentService;

    @Override
    public void updateByEmpId(DayVO dayVO) {
        QueryWrapper<Salary> salaryQueryWrapper = new QueryWrapper<>();
        salaryQueryWrapper.eq("emp_id",dayVO.getEmpId());
        Salary one = this.getOne(salaryQueryWrapper);
        Salary salary = new Salary();
        BeanUtils.copyProperties(one,salary);
        salary.setNewPaidForDay(dayVO.getDay());
        if (salary.getNewPaidForDay() >= salary.getPaidForDay()) {
            salary.setSum(salary.getBasePay());
        }else {
            Integer a = salary.getBasePay()*salary.getNewPaidForDay()/salary.getPaidForDay();
            salary.setSum(a);
        }
        this.updateById(salary);
    }

    @Override
    public void saveSalary(Salary salary) {
        Department byId = departmentService.getById(salary.getDepId());
        salary.setBasePay(byId.getSalary());
        if (salary.getPaidForDay() == null||salary.getPaidForDay() <= 0) {
            salary.setPaidForDay(22);
        }
        this.save(salary);
    }

    @Override
    public MyPageVO<Salary> getSalary(GetSalaryPageVO getSalaryPageVO) {
        MyPageVO<Salary> myPageVO = new MyPageVO<>();
        Page<Salary> page = new Page<>();
        page.setCurrent(getSalaryPageVO.getPageNum());
        page.setSize(getSalaryPageVO.getPageSize());
        this.page(page);
        myPageVO.setPageNum(page.getCurrent());

        myPageVO.setPageSize(page.getSize());
        myPageVO.setTotalPages(page.getPages());
        myPageVO.setTotalItems(page.getTotal());
        myPageVO.setItems(page.getRecords());
        return myPageVO;
    }
}
